package com.ykh.mybbsdemo.controller;

import com.ykh.mybbsdemo.dao.pojo.vo.Result;
import com.ykh.mybbsdemo.constant.VideoConstant;
import com.ykh.mybbsdemo.util.QiniuUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * 图片上传控制器
 */
@RestController
@RequestMapping("upload")
public class UploadController {
    @Autowired
    private QiniuUtils qiniuUtils;

    @PostMapping
    public Result upload(@RequestParam("image") MultipartFile file){
        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
        String key = qiniuUtils.upload(file, fileName);
        if (key!=null){
            return Result.success( VideoConstant.IMAGE_URL + key);
        }
        return Result.fail(20001,"上传失败");
    }


}
